<template>
<!-- pages/user/active_mesg/active_mesg.wxml -->
<view class="logistics_mesg">
    <view class="logistics-wrap">
        <view class="logistics-empty" @tap="clearMessage">清空</view>
        <block v-for="(item, index) in messageList" :key="index">
            <view class="logistics_items">
                <view class="mesg_time"><text>{{item.send_time_text}}</text></view>
                <view class="mesg_cont">
                    <view class="mesg_tit">{{item.message_title}}<text v-if="item.is_see == 0"></text></view>
                    <view class="activemesg_img">
                        <image :src="url + item.img_uri"></image>
                        <text class="activemesg_img text" v-if="item.finished">活动已结束</text>
                    </view>
                    <!-- <text class='mesg_desc' wx:if="{{item.message_content != 0}}">{{item.message_content}}</text> -->
                    <view class="mesg_link" @tap="goDetail" :data-idx="index" :data-item="item">
                        <text>查看详情</text>
                        <image src="/static/images/icon-arrowdown.png"></image>
                    </view>
                </view>
            </view>
        </block>
    </view>
    <view class="mesg_none" v-if="!messageList || messageList.length == 0">
        <image class="mesg_none_img" src="/static/images/error-img.png"></image> 
        <text class="mesg_none_span">暂无消息记录~</text>
    </view> 
</view>
</template>

<script>
// pages/user/active_mesg/active_mesg.js
var app = getApp();
var request = app.globalData.request;
import LoadMore from "../../../utils/LoadMore";
var load = new LoadMore();

export default {
  data() {
    return {
      url: app.globalData.setting.url,
      messageList: null,
      currentPage: 1
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    load.init(this, '', 'messageList');
    this.requestMessageList();
  },
  onShow: function () {
    this.onPullDownRefresh();
  },
  onPullDownRefresh: function () {
    this.resetData();
    this.requestMessageList();
  },
  onReachBottom: function () {
    if (load.canloadMore()) {
      this.requestMessageList();
    }
  },
  methods: {
    //重置数据
    resetData: function () {
      load.resetConfig();
      this.messageList = null;
      this.currentPage = 1;
    },
    requestMessageList: function () {
      var that = this;
      var requestUrl = that.url + '/index.php?m=Api&c=Message&a=message_notice_detail&type=1';
      requestUrl = requestUrl + '&p=' + that.currentPage;
      load.request(requestUrl, function (res) {
        that.currentPage++; //that.setData({ messageList: res.data.result });

        uni.stopPullDownRefresh();
      });
    },
    //删除操作
    clearMessage: function () {
      if (this.messageList == null || this.messageList.length == 0) {
        app.globalData.showTextWarining("当前无消息");
        return;
      }

      var that = this;
      uni.showModal({
        title: '确定清空全部消息！',
        cancelText: '取消',
        confirmText: '确定',
        success: function (res) {
          if (!res.confirm) {
            return;
          }

          that.deleteMsg();
        }
      });
    },
    deleteMsg: function () {
      var that = this;
      request.post('/index.php?m=Api&c=Message&a=deletedMessage&type=1', {
        success: function (res) {
          that.onPullDownRefresh();
        }
      });
    },
    goDetail: function (e) {
      var item = e.currentTarget.dataset.item;
      this.messageList[e.currentTarget.dataset.idx].is_see = 1;
      this.setData({
        messageList: this.messageList
      });

      if (item.finished) {
        this.setMessageForRead(item.rec_id);
        app.globalData.showTextWarining("活动已结束");
        return;
      }

      if (item.is_start == 0) {
        this.setMessageForRead(item.rec_id);
        app.globalData.showTextWarining("活动开始时间:" + item.start_time);
        return;
      }

      switch (item.prom_type) {
        case 1:
          //抢购
          this.setMessageForRead(item.rec_id);

          if (item.goods_id == 0) {
            //表示列表（多个商品）
            uni.navigateTo({
              url: '/pages/activity/seckill_list/seckill_list'
            });
          } else {
            //表示单个商品
            uni.navigateTo({
              url: '/pages/goods/goodsInfo/goodsInfo?goods_id=' + item.goods_id + '&item_id=' + item.item_id
            });
          }

          break;

        case 2:
          //团购
          this.setMessageForRead(item.rec_id);

          if (item.goods_id == 0) {
            //表示列表（多个商品）
            uni.navigateTo({
              url: '/pages/activity/group_list/group_list'
            });
          } else {
            //表示单个商品
            uni.navigateTo({
              url: '/pages/goods/goodsInfo/goodsInfo?goods_id=' + item.goods_id + '&item_id=' + item.item_id
            });
          }

          break;

        case 3:
          //商品优惠
          this.setMessageForRead(item.rec_id);

          if (item.goods_id == 0) {
            //表示列表（多个商品）
            uni.navigateTo({
              url: '/pages/activity/sales_list/sales_list'
            });
          } else {
            //表示单个商品
            uni.navigateTo({
              url: '/pages/goods/goodsInfo/goodsInfo?goods_id=' + item.goods_id + '&item_id=' + item.item_id
            });
          }

          break;

        case 4:
          //预售
          this.setMessageForRead(item.rec_id);
          uni.navigateTo({
            url: '/pages/goods/goodsInfo/goodsInfo?goods_id=' + item.goods_id + '&item_id=' + item.item_id
          });
          break;

        case 5:
          //虚拟
          this.setMessageForRead(item.rec_id);

          if (item.goods_id != 0) {
            uni.navigateTo({
              url: '/pages/goods/goodsInfo/goodsInfo?goods_id=' + item.goods_id + '&item_id=' + item.item_id
            });
          }

          break;

        case 6:
          //拼团
          this.setMessageForRead(item.rec_id);

          if (item.goods_id == 0) {
            //表示列表（多个商品）
            uni.navigateTo({
              url: '/pages/team/index/index'
            });
          } else {
            //表示单个商品
            uni.navigateTo({
              url: '/pages/team/team_info/team_info?goods_id=' + item.goods_id + '&item_id=' + (item.item_id ? item.item_id : 0) + '&team_id=' + item.prom_id
            });
          }

          break;

        case 7:
          //搭配购
          this.setMessageForRead(item.rec_id);
          app.globalData.showTextWarining("暂无搭配购");
          break;

        case 8:
          //自定义图文消息
          this.setMessageForRead(item.rec_id);
          uni.navigateTo({
            url: '/pages/goods/goodsInfo/goodsInfo?goods_id=' + item.goods_id + '&item_id=' + item.item_id
          });
          break;

        case 9:
          //订单促销
          this.setMessageForRead(item.rec_id); //app.showTextWarining("下单即可享受优惠活动");

          uni.navigateTo({
            url: 'msg_info/msg_info?rec_id=' + item.rec_id
          });
          break;
      }
    },
    setMessageForRead: function (recId) {
      request.post('/index.php?m=Api&c=Message&a=setMessageForRead', {
        data: {
          rec_id: recId
        },
        success: function (res) {}
      });
    }
  }
};
</script>
<style>

page{
  background-color: #efefef;
}
.logistics_mesg{ 
    width:100%; 
    height:auto; 
    padding-bottom:60rpx;
}
.logistics_items{ 
    width:92%; 
    padding:0 4%;
}
.logistics_items .mesg_time{ 
    line-height:66rpx; 
    padding-top:18rpx; 
    font-size:24rpx; 
    color:#666; 
    text-align:center;
}
.mesg_cont{ 
    width:94%; 
    background-color:#fff; 
    border:1px solid #f3f3f3;
    border-radius:10rpx; 
    padding:30rpx 3% 0; 
    text-align:left;
}
.mesg_cont .mesg_tit{ 
    font-size:30rpx; 
    color:#333; 
    position:relative; 
    overflow:hidden;
    text-overflow:ellipsis;
    white-space: nowrap; 
    padding-right:30px;
    margin-bottom: 15rpx;
}
.mesg_cont .mesg_tit text{ 
    width:18rpx; 
    height:18rpx; 
    background-color:#ff3939; 
    border-radius:50%; 
    position:absolute; 
    right:6rpx; 
    top:6rpx;
}
.mesg_cont .activemesg_img{ 
    width:100%; 
    height:280rpx; 
    position:relative;
    padding-top: 24rpx;
    text-align: center;
}
.mesg_cont .activemesg_img image{
    width:100%;
    height:100%;
   vertical-align: middle;
}
.mesg_cont .mesg_desc{ 
    font-size:24rpx; 
    color:#333; 
    line-height:36rpx; 
    padding:18rpx 6rpx 0;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
}
.mesg_cont .mesg_link{ 
    display:block; 
    width:100%; 
    height:66rpx; 
    line-height:66rpx; 
    padding-top: 24rpx;
    border-top:1px solid #f3f3f3; 
}
.mesg_cont .mesg_link text{
    float: left;
    color:#999; 
    font-size:24rpx; 
}
.mesg_cont .mesg_link image{
    float: right;
    width: 25rpx;
    height: 30rpx;
    margin-top: 18rpx;
}
.mesg_cont .activemesg_img text{ 
    width:100%; 
    height:100%; 
    background-color:rgba(0,0,0,0.6); 
    position:absolute; 
    left:0; 
    top:0; 
    z-index:9; 
    line-height:280rpx; 
    font-size:36rpx; 
    color:#fff; 
    font-weight:bold; 
    letter-spacing:6rpx;
    margin-top: 0；
}
.logistics-wrap{
  position: relative
}
.logistics-empty{
    position: absolute;
    color: #666666;
    font-size: 24rpx;
    right: 30rpx;
    top: 25rpx;
    line-height: 26rpx;
    border: 1rpx solid #f3f3f3;
    border-radius:8rpx; 
    padding: 6rpx;

}
.mesg_none{ 
    width:100%; 
     position: fixed; 
    text-align:center;
    height:100vh;
    background-color:#fff;
    z-index: 9;
  }
.mesg_none_img{
    height:450rpx;
    margin-top:180rpx;
    }
.mesg_none_span{
    font-size:30rpx;
    margin-top: 30rpx;
    color:#666;
    display:block;
    }




</style>